#include "main.h"
#include "color.h"
#include "manager.h"
void manager_menu()//管理员界面
{
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_YELLOW "\t♥♥♥♥♥♥♥♥♥♥♥    1.增加菜品     ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_RED "\t♥♥♥♥♥♥♥♥♥♥♥    2.删除菜品     ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_BLUE "\t♥♥♥♥♥♥♥♥♥♥♥    3.查看菜品     ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_GREEN "\t♥♥♥♥♥♥♥♥♥♥♥    4.修改价格     ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_PURPLE "\t♥♥♥♥♥♥♥♥♥♥♥    5.修改密码     ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_WHITE "\t♥♥♥♥♥♥♥♥♥♥♥    0.返回上一级   ♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
    printf(FONT_HIGHLIGHT_CYAN "\t♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥" DEFAULT_MODE "\n");
}
void manager_choose(ManagerLinkList L, ElemType e, int *sum_1, Mana *A, Mana *B)//管理员菜单
{
    // struct manager A;
    // struct manager B;
    // int sum_1 = 1;
    int sum = 3;
    manager_account(A, sum_1);
password:
    if (manager_Input_password(A, B) == 0)
        return;
    else
    {
        int input = 0;
        do
        {
            manager_menu();
            printf(FONT_HIGHLIGHT_CYAN BLINK"请选择想要进行的操作："DEFAULT_MODE);
            scanf("%d", &input);
            system("clear");
            while (getchar() != '\n')
                ;
            switch (input)
            {
            case 1:
                manager_add_cuisine(L);
                break;
            case 2:
                manager_del_cuisine(L, e);
                break;
            case 3:
                Listshow_Manager(L);
                break;
            case 4:
                manager_change_price(L, e);
                break;
            case 5:
                if (manager_change_password(A, B) == 1)
                    ;
                goto password;
                break;
            case 0:
                printf(FONT_PURPLE "\t正在返回..." DEFAULT_MODE "\n");
                sleep(2);
                break;
            default:
                printf(FONT_HIGHLIGHT_RED "\t输入错误！请重新输入!\n" DEFAULT_MODE);
                break;
            }
        } while (input);
    }
}
bool InitList_Manager(ManagerLinkList *L)//初始化链表
{
    *L = (ManagerLinkList)malloc(sizeof(ManagerNode));
    if (*L == NULL)
    {
        printf("malloc ManagerLinkList error\n");
        return false;
    }
    memset((*L)->data.introduction, 0, 100);
    memset((*L)->data.name, 0, 24);
    (*L)->data.price = 0;
    (*L)->prev = *L;
    (*L)->next = *L;
    return true;
}
bool Listinsert_Sort_Manager(ManagerLinkList L, ElemType e)//增加节点
{
    ManagerLinkList p = L;
    ManagerLinkList new_node = NULL;
    while (p->next != L && (p->next->data.num < e.num)) // 查找比e大的结点  的前一结点， p 指向该结点
    {
        p = p->next;
    }
    new_node = (ManagerLinkList)malloc(sizeof(ManagerNode));
    if (new_node == NULL)
    {
        printf("malloc new_node error\n");
        return false;
    }
    new_node->data = e;
    new_node->next = p->next;
    p->next->prev = new_node;
    p->next = new_node;
    new_node->prev = p;
    L->data.num++;
    return true;
}
bool ListDelete_Manager(ManagerLinkList L, unsigned int i)//删除节点
{
    ManagerLinkList p = L;
    ManagerLinkList delete_node = NULL;
    int flag = 0;
    while (p->next != L && p->next->data.num != i) // 查找第 i-1 个结点， p 指向该结点
    {
        flag++;
        p = p->next;
    }
    if (flag == L->data.num)
        return false;
    delete_node = p->next;
    p->next = delete_node->next;
    delete_node->next->prev = p;
    free(delete_node);
    L->data.num--;
    return true;
}
bool ElemModify_Manager(ManagerLinkList L, int e, int price)//查找节点
{
    ManagerLinkList p = L->next;
    int j = 0;
    while (p != L && p->data.num != e) //  查找第 i个结点， p指向该结点
    {
        p = p->next;
    }
    if (p->data.num != e)
        return false;
    p->data.price = price;
    return true;
}
void Listshow_Manager(ManagerLinkList L)//遍历链表
{
    ManagerLinkList p = L->next;
    while (p != L)
    {
        printf("\n");
        printf(FONT_HIGHLIGHT_BLUE"\t序号:%d \t菜名:%s \t介绍:%s  \t价格:%d"DEFAULT_MODE, p->data.num, p->data.name, p->data.introduction, p->data.price);
        printf("\n");
        
        p = p->next;
    }
    printf("\n");
}
void manager_add_cuisine(ManagerLinkList L)//管理员添加菜品
{
    ManagerLinkList p = L;
    ElemType e = {0};
    printf(FONT_HIGHLIGHT_GREEN"请输入序号："DEFAULT_MODE);
    scanf("%d", &e.num);
    printf(FONT_HIGHLIGHT_BLUE"请输入菜品名称："DEFAULT_MODE);
    scanf("%s", e.name);
    printf(FONT_HIGHLIGHT_YELLOW"请输入菜品的介绍："DEFAULT_MODE);
    scanf("%s", e.introduction);
    printf(FONT_HIGHLIGHT_CYAN"请输入菜品的价格："DEFAULT_MODE);
    scanf("%d", &e.price);
    Listinsert_Sort_Manager(p, e);
    printf(FONT_HIGHLIGHT_RED"添加成功"DEFAULT_MODE"\n");
}
void manager_del_cuisine(ManagerLinkList L, ElemType e)//管理员删除菜品
{
    unsigned int num;
    printf(FONT_HIGHLIGHT_YELLOW"请输入要删除的菜品序号："DEFAULT_MODE);
    scanf("%d", &num);
    if (ListDelete_Manager(L, num))
        printf(FONT_HIGHLIGHT_RED"删除成功！"DEFAULT_MODE"\n");
    else
        printf("未找到删除对象!请重试\n");
}
void manager_change_price(ManagerLinkList L, ElemType e)//管理员修改价格
{
    int num, price;
    printf(FONT_HIGHLIGHT_CYAN"请输入要修改的菜品的序号："DEFAULT_MODE);
    scanf("%d", &num);
    printf(FONT_HIGHLIGHT_GREEN"请输入要修改的价格："DEFAULT_MODE);
    scanf("%d", &price);
    if (ElemModify_Manager(L, num, price))
        printf(FONT_HIGHLIGHT_RED"修改成功！"DEFAULT_MODE"\n");
    else
        printf(FONT_HIGHLIGHT_RED"未找到修改对象！请重试"DEFAULT_MODE"\n");
}
void manager_account(Mana *A, int *sum_1)//管理员注册账户
{
    if ((*sum_1) == 1)
    {
        printf(FONT_HIGHLIGHT_YELLOW"请输入注册账号："DEFAULT_MODE);
        scanf("%s", A->account);
        while (getchar() != '\n')
            ;
        printf(FONT_HIGHLIGHT_YELLOW"请输入密码："DEFAULT_MODE);
        scanf("%s", A->password);
        while (getchar() != '\n')
            ;
        printf(FONT_HIGHLIGHT_RED"注册成功！"DEFAULT_MODE"\n");
        (*sum_1)--;
    }
}
bool manager_Input_password(Mana *A, Mana *B)//管理员登录账户
{
    int sum = 3;
    while (sum)
    {
        printf(FONT_HIGHLIGHT_CYAN"请输入登录账号："DEFAULT_MODE);
        scanf("%s", B->account);
        printf(FONT_HIGHLIGHT_CYAN"请输入密码："DEFAULT_MODE);
        scanf("%s", B->password);
        if ((strcmp(A->account, B->account) == 0) && (strcmp(A->password, B->password) == 0))
        {

            printf(FONT_HIGHLIGHT_RED"登录成功！"DEFAULT_MODE"\n");
            sum = 0;
            return true;
        }
        else
        {
            printf(FONT_HIGHLIGHT_RED"密码错误!"DEFAULT_MODE"\n");
            sum--;
        }
    }
    if (sum == 0)
    {
        printf(FONT_HIGHLIGHT_PURPLE"密码已输错三次，准备返回..."DEFAULT_MODE"\n");
        return false;
    }
}
bool manager_change_password(Mana *A, Mana *B)//管理员修改密码
{
    int sum = 3;
    while (sum)
    {
        printf("请输入原来的密码：");
        scanf("%s", A->password);
        if ((strcmp(A->password, B->password) == 0))
        {
            printf("请输入修改后的密码：");
            scanf("%s", A->password);
            printf("修改密码成功，请重新登录！\n");
            return true;
        }
        else
        {
            printf(FONT_HIGHLIGHT_RED"密码错误！"DEFAULT_MODE"\n");
            sum--;
        }
    }
    if (sum == 0)
    {
        printf(FONT_HIGHLIGHT_RED"密码已输错三次，准备返回..."DEFAULT_MODE"\n");
        sleep(2);
        return false;
    }
}